﻿using System;
using System.Data;
using System.Data.DBLib;
using SysComm;

namespace SysOperationLayer.DBHandle
{
    /// <summary>
    /// 
    /// </summary>
    public class AgentsDB : DBBase
    {
        /// <summary>
        /// 获取agent列表
        /// </summary>
        /// <param name="agent"></param>
        /// <param name="chnName"></param>
        /// <param name="name"></param>
        /// <param name="contactPerson"></param>
        /// <param name="isDelete"></param>
        /// <returns></returns>
        public DataTable getAgentsList(string agent, string chnName, string name, string contactPerson, int isDelete)
        {
            if (ConnectDB())
            {
                string strSql = "select * from Agent where 1=1";

                if (agent != "" && agent != null)
                {
                    strSql += " and Agent like '%" + agent + "%'";
                }
                if (chnName != "" && chnName != null)
                {
                    strSql += " and ChineseName like '%" + chnName + "%'";
                }
                if (name != "" && name != null)
                {
                    strSql += " and Name like '%" + name + "%'";
                }
                if (contactPerson != "" && contactPerson != null)
                {
                    strSql += " and ContactPerson like '%" + contactPerson + "%'";
                }
                strSql += " and IsDelete=" + isDelete;
                return m_objDB.getDataTable(strSql, "Agent");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public DataRow GetItem(string strAgent)
        {
            if (ConnectDB())
            {
                string strSql = "select * from Agent where Agent='" + strAgent + "'";
                return m_objDB.getDataRow(strSql, "Agent");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public int Insert(string agent,string name,string chineseName,string address,
                                string webStite,string jobTitle,
                                string contactPerson,string phone,string fax,string email,
                                string contactPerson2,string phone2,string fax2,string email2,
                                string strUserID, string strUserIP)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "insert into Agent(Agent, Name, ChineseName, Address, WebSite, JobTitle, ContactPerson, Phone, Fax, EmailAddress ,ContactPerson2, Phone2, Fax2, EmailAddress2";
                strSql += ", CreateIp, CreateBy, CreateDate, UpdateIp, UpdateBy, UpdateDate)";
                strSql += " values('" + agent + "','" + name + "','" + chineseName + "','" + address + "','" + webStite + "','" + jobTitle + "','" + contactPerson + "','" + phone + "','" + fax + "','" + email + "','" + contactPerson2 + "','" + phone2 + "','" + fax2 + "','" + email2 + "','";
                strSql += strUserIP + "','" + strUserID + "',getDate(),'" + strUserIP + "','" + strUserID + "','" + DateTime.Now + "')";

                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Update(string agent, string name, string chineseName, string address,
                                string webStite, string jobTitle,
                                string contactPerson, string phone, string fax, string email,
                                string contactPerson2, string phone2, string fax2, string email2,
                                string strUserID, string strUserIP)
        {
            if (IsEmpty(agent))
            {
                m_strMessage = "Agent Code is Empty!";
            }
            else if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update Agent set ";
                strSql += " Name='" + name + "'";
                strSql += ", ChineseName='" + chineseName + "'";
                strSql += ", Address='" + address + "'";
                strSql += ", WebSite='" + webStite + "'";
                strSql += ", JobTitle='" + jobTitle + "'";
                strSql += ", ContactPerson='" + contactPerson + "'";
                strSql += ", Phone='" + phone + "'";
                strSql += ", Fax='" + fax + "'";
                strSql += ", EmailAddress='" + email + "'";
                strSql += ", ContactPerson2='" + contactPerson2 + "'";
                strSql += ", Phone2='" + phone2 + "'";
                strSql += ", Fax2='" + fax2 + "'";
                strSql += ", EmailAddress2='" + email2 + "'";
                strSql += ", UpdateBy='" + strUserID + "'";
                strSql += ", UpdateIp='" + strUserIP + "'";
                strSql += ", UpdateDate=getDate()";
                strSql += " where Agent='" + agent + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Delete(string agent, string strUserID, string strUserIP)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update Agent set ";
                strSql += " isDelete=1";
                strSql += ", DeleteBy='" + strUserID + "'";
                strSql += ", DeleteIP='" + strUserIP + "'";
                strSql += ", DeleteDate=getDate()";
                strSql += " where Agent='" + agent + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }

            return -1;
        }

        public int Trash(string projectClass)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "delete from Agent where Agent='" + projectClass + "'";

                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }

            return -1;
        }

        public int Restore(string agent)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "update Agent set isDelete = 0 where Agent = '" + agent + "'";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }

            return -1;
        }
    }
}
